<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="订单名称" prop="orderName">
        <el-input v-model="queryParams.orderName" placeholder="请输入订单名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择">
          <el-option label="待支付定金" value="0">待支付定金</el-option>
          <el-option label="待支付尾款" value="1">待支付尾款</el-option>
          <el-option label="完成" value="2">完成</el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['market:market:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['market:market:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['market:market:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['market:market:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="marketList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="订单编号" align="center" prop="orderNo" width="250"/>
      <el-table-column label="订单名称" align="center" prop="orderName"/>
      <el-table-column label="客户" align="center" prop="client.clientName"/>
      <el-table-column label="销售数量" align="center" prop="marketNum"/>
      <el-table-column label="单价" align="center" prop="marketPrice"/>
      <el-table-column label="单位" align="center" prop="marketUnit"/>
      <el-table-column label="金额合计" align="center" prop="marketTotal"/>
      <el-table-column label="税额" align="center" prop="marketTax"/>
      <el-table-column label="销售人员" align="center" prop="marketName"/>
      <el-table-column label="状态" align="center" prop="auditStatus" width="140">
        <template slot-scope="scope">
           <span v-if="scope.row.auditStatus == 0">
            <el-tag type="warning">待支付定金</el-tag>
          </span>
          <span v-if="scope.row.auditStatus == 1">
            <el-tag type="danger">待支付尾款</el-tag>
          </span>
          <span v-if="scope.row.auditStatus == 2">
            <el-tag type="success">完成</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作时间" align="center" prop="auditTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="auditPerson"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus==0">
            <el-button size="mini" type="warning" icon="el-icon-loading" @click="handleApprover(scope.row)"
                       v-hasPermi="['market:market:approver']">支付定金</el-button>
          </span>
          <span v-if="scope.row.auditStatus==1">
            <el-button size="mini" type="danger" icon="el-icon-loading" @click="handleAudit(scope.row)"
                       v-hasPermi="['market:market:audit']">支付尾款</el-button>
          </span>
          <span v-if="scope.row.auditStatus==2">
            <el-button size="mini" type="success" icon="el-icon-success" >完成订单</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 支付定金对话框 -->
    <el-dialog :title="title1" :visible.sync="openApprover" width="960x" append-to-body>
      <el-form ref="form" :model="formApprover" :rules="rules" label-width="180px">

        <el-form-item label="订单编号" prop="orderNo" >
          <el-input v-model="formApprover.orderNo" placeholder="请输入订单编号" readOnly/>
        </el-form-item>
        <el-form-item label="订单金额" prop="Price">
          <el-input v-model="formApprover.Price" placeholder="请输入订单金额" readOnly/>
        </el-form-item>
        <el-form-item label="状态" prop="auditStatus">
          <el-radio-group v-model="formApprover.auditStatus">
            <el-radio label="1">已付款</el-radio>
            <el-radio label="0">未付款</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormApprover">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 支付尾款对话框 -->
    <el-dialog :title="title1" :visible.sync="openAudit" width="960x" append-to-body>
      <el-form ref="form" :model="formAudit" :rules="rules" label-width="180px">

        <el-form-item label="订单编号" prop="orderNo" >
          <el-input v-model="formAudit.orderNo" placeholder="请输入订单编号" readOnly/>
        </el-form-item>
        <el-form-item label="订单金额" prop="EndPrice">
          <el-input v-model="formAudit.EndPrice" placeholder="请输入订单金额" readOnly/>
        </el-form-item>
        <el-form-item label="状态" prop="auditStatus">
          <el-radio-group v-model="formAudit.auditStatus">
            <el-radio label="2">已付款</el-radio>
            <el-radio label="1">未付款</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAudit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open" width="60%">
      <el-form :inline="true" ref="form" :model="form" :rules="rules" class="demo-form-inline">
        <el-form-item label="客户：" prop="region">
          <el-select placeholder="选择客户" v-model="form.clientId">
            <el-option v-for="item in clientList"
                       :key="item.clientName"
                       :label="item.clientName"
                       :value="item.clientId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单号：">
          <el-input v-model="form.orderNo" disabled=""></el-input>
        </el-form-item>
        <el-table :data="tableData" border>
          <el-table-column label="订单名称" prop="orderName">
            <template #default="scope">
              <el-input v-model="form.orderName"/>
            </template>
          </el-table-column>
          <el-table-column label="销售数量" prop="marketNum">
            <template #default="scope">
              <el-input v-model="form.marketNum"/>
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="marketPrice">
            <template #default="scope">
              <el-input v-model="form.marketPrice"/>
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="marketUnit">
            <template #default="scope">
              <el-input v-model="form.marketUnit"/>
            </template>
          </el-table-column>
        </el-table>
        <div class="top">
          <el-form-item label="金额合计：">
            <el-input v-model="form.marketTotal"></el-input>
          </el-form-item>
          <el-form-item label="税额：">
            <el-input v-model="form.marketTax"></el-input>
          </el-form-item>
          <el-form-item label="待收金额：">
            <el-input v-model="form.receivableAmount"></el-input>
          </el-form-item>
          <el-form-item label="本次收款：">
            <el-input v-model="form.nowCollection"></el-input>
          </el-form-item>
        </div>
      </el-form>
      <div class="right">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改销售列表对话框 -->
<!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单名称" prop="orderName">
          <el-input v-model="form.orderName" placeholder="请输入订单名称"/>
        </el-form-item>
        <el-form-item label="客户id" prop="clientId">
          <el-input v-model="form.clientId" placeholder="请输入客户id"/>
        </el-form-item>
        <el-form-item label="销售数量" prop="marketNum">
          <el-input v-model="form.marketNum" placeholder="请输入销售数量"/>
        </el-form-item>
        <el-form-item label="单价" prop="marketPrice">
          <el-input v-model="form.marketPrice" placeholder="请输入单价"/>
        </el-form-item>
        <el-form-item label="单位" prop="marketUnit">
          <el-input v-model="form.marketUnit" placeholder="请输入单位"/>
        </el-form-item>
        <el-form-item label="金额合计" prop="marketTotal">
          <el-input v-model="form.marketTotal" placeholder="请输入总金额"/>
        </el-form-item>
        <el-form-item label="税额" prop="marketTotal">
          <el-input v-model="form.marketTax" placeholder="请输入税额"/>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>-->
  </div>
</template>

<script>
  import {
    approverMarket,
    auditMarket,
    listMarket,
    getMarket,
    delMarket,
    addMarket,
    updateMarket
  } from "@/api/market/market";
  import {listClient} from "../../../api/client/client";

  export default {
    name: "Market",
    data() {
      return {
        tableData:[
          { orderName:"",
            marketPrice:"",
            marketUnit:""
          }
        ],
        clientList:[],
        textarea: '',
        formApprover: {
          marketId: '',
          orderNo:'',
          EndPrice:'',
        },
        openApprover: false,
        openReturn: false,
        formAudit: {
          orderNo:'',
          EndPrice:'',
          marketId: ''
        },
        openAudit: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 销售列表表格数据
        marketList: [],
        // 弹出层标题
        title: "",
        title1: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderName: null,
          clientId: null,
          marketNum: null,
          marketPrice: null,
          marketUnit: null,
          marketTotal: null,
          marketTax: null,
          orderStatus: null,
          auditStatus: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      };
    },
    created() {
      this.getList();
    },
    methods: {
      listClients() {
        listClient(null).then(response => {
          this.clientList = response.rows;
        });
      },
      handleApprover(row) {
        this.formApprover.marketId = row.marketId;
        this.formApprover.orderNo = row.orderNo;
        // 调用获取当前日期的方法加四位随机数  赋值表单中的项目编号
        this.formApprover.orderNo = this.getProjectNum() + Math.floor(Math.random() * 10000)  // 如果是6位或者8位随机数，相应的 *1000000或者 *100000000就行了


        this.formApprover.Price=row.marketPrice*row.marketNum*0.10;
        this.openApprover = true;
        this.title1 = '审核是否支付定金'

      },
      submitFormApprover() {
        approverMarket(this.formApprover).then(response => {
          this.$modal.msgSuccess("支付定金成功");
          this.openApprover = false;
          this.getList();
        })
      },
      // 获取当前日期的方法
      getProjectNum() {
        const projectTime = new Date() // 当前中国标准时间
        const Year = projectTime.getFullYear() // 获取当前年份 支持IE和火狐浏览器.
        const Month = projectTime.getMonth() + 1 // 获取中国区月份
        const Day = projectTime.getDate() // 获取几号
        var CurrentDate = Year
        if (Month >= 10) { // 判断月份和几号是否大于10或者小于10
          CurrentDate += Month
        } else {
          CurrentDate += '0' + Month
        }
        if (Day >= 10) {
          CurrentDate += Day
        } else {
          CurrentDate += '0' + Day
        }
        return CurrentDate
      },
      submitFormAudit() {
        auditMarket(this.formAudit).then(response => {
          this.$modal.msgSuccess("支付尾款成功");
          this.openAudit = false;
          this.getList();
        })
      },
      handleAudit(row) {
        this.formAudit.marketId = row.marketId;
        this.formAudit.orderNo = row.orderNo;
        // 调用获取当前日期的方法加四位随机数  赋值表单中的项目编号
        // this.formAudit.orderNo = this.getProjectNum() + Math.floor(Math.random() * 10000)  // 如果是6位或者8位随机数，相应的 *1000000或者 *100000000就行了
        this.formAudit.orderNo = ("JZ"+((Math.random() * 9 + 1) * 100000000)).substr(0,11);
        const price=row.marketPrice*row.marketNum*0.10
        console.log(price);
        this.formAudit.EndPrice=row.marketPrice*row.marketNum-price;
        this.openAudit = true;
        this.title1 = '审核是否支付尾款'
      },
      /** 查询销售列表列表 */
      getList() {
        this.loading = true;
        listMarket(this.queryParams).then(response => {
          this.marketList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          marketId: null,
          marketName: null,
          clientId: null,
          marketNum: null,
          marketPrice: null,
          marketUnit: null,
          auditStatus: null,
          auditOpinion: null,
          auditTime: null,
          auditPerson: null,
          approverOpinion: null,
          approverTime: null,
          approverPerson: null,
          description: null,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.marketId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.form.orderNo = ("JZ"+((Math.random() * 9 + 1) * 100000000)).substr(0,11);
        this.listClients();
        this.open = true;
        this.title = "添加销售列表";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const marketId = row.marketId || this.ids
        getMarket(marketId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改销售列表";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.marketId != null) {
              updateMarket(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addMarket(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const marketIds = row.marketId || this.ids;
        this.$modal.confirm('是否确认删除销售列表编号为"' + marketIds + '"的数据项？').then(function () {
          return delMarket(marketIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('market/market/export', {
          ...this.queryParams
        }, `market_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>

<style>
  .demo-form-inline {
    text-align: left;
  }
  .top{
    margin-top: 20px;
  }
  .right{
    text-align: right;
  }
</style>
